package com.pi;


import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {


    private static MessageDigest md = null;


    /**
     * 如果目录不存在则创建目录
     *
     * @param file
     */
    public static void createFileIfNotExists(File file) {
        if (null != file) {
            if (file.isFile()) {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
            } else {
                file.mkdirs();
            }
        }
    }


    /**
     * 文件对比
     *
     * @param file1
     * @param file2
     * @return
     */
    public static boolean compare(File file1, File file2) {

        if ((file1 != null) && (file2 != null)) {
            if (file1.getAbsolutePath().equals(file2.getAbsolutePath())) {
                return true;
            }
            if ((file1.isFile()) && (file2.isFile())) {
                if (file1.length() == file2.length()) {
                    return getFileMD5(file1).equals(getFileMD5(file2));
                }
            } else if ((file1.isDirectory()) && (file1.isDirectory())) {
                return file1.getName().equals(file2.getName());
            }
        }
        return false;
    }


    /**
     * 获得文件MD5
     *
     * @param file
     * @return
     */
    public static String getFileMD5(File file) {

        if (null == md) {
            try {
                md = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e1) {
                e1.printStackTrace();
            }
        }
        if (file != null) {

            InputStream is = null;
            try {
                is = new FileInputStream(file);
                byte[] by = new byte[1024];
                int len = -1;
                while ((len = is.read(by)) != -1) {
                    md.update(by, 0, len);
                }
                byte[] digest = md.digest();
                BigInteger bi = new BigInteger(1, digest);
                return bi.toString(16);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        return null;
    }

    /**
     * 获得新版本的相对于老版本的文件路径
     *
     * @param file
     * @param baseDirPath
     * @param newDirPath
     * @return
     */
    public static File getBaseFile(File file, String baseDirPath, String newDirPath) {
        if (file != null) {
            String filePath = file.getAbsolutePath();

            String childPath = filePath.substring(baseDirPath.length(),
                    filePath.length());
            childPath = newDirPath + childPath;
            return new File(childPath);
        }
        return null;
    }


    /**
     * 获得所有文件
     *
     * @param file
     * @param constainer
     * @return
     */
    private static List<File> listAllFiles(File file, List<File> constainer) {

        if (null == constainer) {
            constainer = new ArrayList<>();
        }

        if (file.isFile()) {
            constainer.add(file);
        } else {
            for (File childFile : file.listFiles()) {
                listAllFiles(childFile, constainer);
            }
        }
        return constainer;
    }

    /**
     * 获得目录下所有文件
     *
     * @param file
     * @return
     */
    public static List<File> listAllFiles(File file) {
        return listAllFiles(file, null);
    }



}
